package com.css.Thread;

//线程不安全案例01

public class syn01 {
    public static void main(String[] args) {
        BuyTicket buyTicket=new BuyTicket();
        new Thread(buyTicket,"css").start();
        new Thread(buyTicket,"dd").start();
        new Thread(buyTicket,"fc").start();
    }
}
class BuyTicket implements Runnable{
    private Integer ticketNum=10;
    private boolean flag=true;//外部停止线程


    public void run() {
        while (flag){
            buy();
        }

    }
    //锁的是this
    private  void buy(){
        synchronized (ticketNum){
            if (ticketNum<=0){
                flag=false;
                return;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"->"+ticketNum--);
        }


    }
}
